package l.l.w.leetcode._054SpiralMatrix;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by llw on 2017/11/16.
 */
public class _054SpiralMatrix {

    public List<Integer> spiralOrder(int[][] matrix) {
        if(matrix ==null || matrix.length <1) return Collections.emptyList();
        int i = 0, j = 0, startI = 0, startJ = 0, endI = matrix.length, endJ = matrix[0].length;
        List<Integer> list = new ArrayList<Integer>(endI * endJ);
        while(startI <= endI && startJ < endJ){
            for( ;j >= startJ && j < endJ; ++j){
                list.add(matrix[i][j]);
            }
            startI++;
            i = startI;
            j--;//跳出循环时 j = endJ
            for(;i >= startI && i < endI; ++i){
                list.add(matrix[i][j]);
            }
            endJ--;
            j = endJ - 1;
            i--;
            for(;j >= startJ && j < endJ; --j){
                list.add(matrix[i][j]);
            }
            endI--;
            i = endI - 1;
            j = startJ;
            for(;i >= startI && i < endI; --i){
                list.add(matrix[i][j]);
            }
            startJ++;
            j = startJ;
            i = startI;
        }
        return list;
    }

    public static void main(String[] args) {
        int[][] a = {{2,3}};
        List<Integer> result = new _054SpiralMatrix().spiralOrder(a);
    }
}
